package org.secangkirkopipanas.gxtcore.server.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.secangkirkopipanas.gxtcore.common.exception.DataNotFoundException;
import org.secangkirkopipanas.gxtcore.server.dao.ISystemBusinessUnitDAO;
import org.secangkirkopipanas.gxtcore.server.domain.SystemBusinessUnit;
import org.secangkirkopipanas.gxtcore.server.domain.SystemGroup;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class SystemBusinessUnitDAO implements ISystemBusinessUnitDAO {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void add(SystemBusinessUnit systemBusinessUnit) {
		sessionFactory.getCurrentSession().saveOrUpdate(systemBusinessUnit);
	}

	public SystemBusinessUnit get(long unitId) {
		return (SystemBusinessUnit) sessionFactory.getCurrentSession()
				.createCriteria(SystemBusinessUnit.class).add(
						Restrictions.idEq(unitId)).uniqueResult();
	}

	public SystemBusinessUnit get(String unitName) {
		return (SystemBusinessUnit) sessionFactory.getCurrentSession()
				.createCriteria(SystemBusinessUnit.class).add(
						Restrictions.eq("name", unitName)).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<SystemBusinessUnit> getForList() {
		return sessionFactory.getCurrentSession().createCriteria(
				SystemBusinessUnit.class).list();
	}

	@SuppressWarnings("unchecked")
	public List<SystemBusinessUnit> getBusinessUnitMembers(
			SystemBusinessUnit systemBusinessUnit) {
		return sessionFactory.getCurrentSession().createCriteria(
				SystemGroup.class).add(
				Restrictions.eq("businessUnit", systemBusinessUnit)).list();
	}

	public int getTotalCount() {
		return (Integer) sessionFactory.getCurrentSession().createCriteria(
				SystemGroup.class).setProjection(Projections.rowCount()).list()
				.get(0);
	}

	public void add(String systemBusinessUnitName) {
		SystemBusinessUnit systemBusinessUnitFound = get(systemBusinessUnitName);
		if (systemBusinessUnitFound != null) {
			sessionFactory.getCurrentSession().delete(systemBusinessUnitFound);
		} else {
			throw new DataNotFoundException("User with username '"
					+ systemBusinessUnitName + "' is not found");
		}
	}

	public void delete(SystemBusinessUnit systemBusinessUnit) {
		SystemBusinessUnit systemBusinessUnitFound = get(systemBusinessUnit
				.getName());
		if (systemBusinessUnitFound != null) {
			sessionFactory.getCurrentSession().delete(systemBusinessUnitFound);
		} else {
			throw new DataNotFoundException("User with username '"
					+ systemBusinessUnit.getName() + "' is not found");
		}

	}

}